package com.spring.crm.filter;

import com.spring.crm.conf.TokenUtil;
import javafx.application.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Order(1)
@Component
@WebFilter(urlPatterns = "/*")
public class crmFilter  implements Filter {
    private final static Logger LOGGER = LoggerFactory.getLogger(Application.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        LOGGER.info("过滤器初始化");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        LOGGER.info("------------filter------------");
        HttpServletRequest httprequest = (HttpServletRequest) request;
        HttpServletResponse httpresponse = (HttpServletResponse) response;
        httpresponse.setHeader("Access-Control-Allow-Origin", "*");
        httpresponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpresponse.setHeader("Access-Control-Allow-Methods", "GET,POST,PATCH,PUT,OPTIONS,DELETE");
        httpresponse.setHeader("Access-Control-Allow-Headers", "Origin,Content-Type,Cookie,Accept,Token");
        if(!httprequest.getRequestURL().toString().equals("/api/crm/user/login")){
            LOGGER.info("------------token------------");
            //Integer message = TokenUtil.verify(httprequest.getHeader("token"));

        }
        chain.doFilter(request, response);
    }

    /**
     * 在销毁Filter时自动调用。
     */
    @Override
    public void destroy() {
        LOGGER.info("过滤器销毁");
    }
}
